<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Fenxiao extends User_Controller {

	public function __construct()
    {
        parent::__construct();
        // Your own constructor code
    }

    public function index()
    {
        $this->smarty->assign("menu", "orders");
        $this->smarty->display("user/fx_index.html");
    }

    /**
     * 我的下级同学
     * @return [type] [description]
     */
    function flist($level = 1,$limit = 0){
    	$this->load->model('User_model');
    	$this->smarty->assign("level",$level);
    	$this->smarty->assign("user_id",$this->user_data['id']);

        if($limit > 0 && is_ajax_request()) $limit = ($limit - 1) * $this->offset;
    	//获取我的一级同学
    	if($level == 1){
    		$sub_user = $this->User_model->get_user_by_parent1($this->user_data['id'],$limit , $this->offset);
    	}elseif($level == 2){
    		$sub_user = $this->User_model->get_user_by_parent2($this->user_data['id'],$limit , $this->offset);
    	}elseif($level == 3){
    		$sub_user = $this->User_model->get_user_by_parent3($this->user_data['id'],$limit , $this->offset);
    	}

        $this->smarty->assign("level", $level);
        if(is_ajax_request()){
            if(!empty($sub_user['result'])){
                foreach ($sub_user['result'] as $key => $value) {
                    $sub_user['result'][$key]['add_time'] = date("Y-m-d", $value['add_time']);
                    if(is_null($sub_user['result'][$key]['nick'])){
                        $sub_user['result'][$key]['nick'] = '';
                    }
                }
                echo json_encode($sub_user['result']);
            }else{
                echo json_encode(array());
            }
            exit;
        }
    	$this->smarty->assign("sub_user", $sub_user['result']);
    	// $this->smarty->assign("sub1_user_count", $sub_user['total_count']);
    	/**
    	//获取我的二级同学
    	$sub2_user = $this->User_model->get_user_by_parent2($this->user_data['id']);
    	$this->smarty->assign("sub1_user", $sub2_user['result']);
    	$this->smarty->assign("sub1_user_count", $sub2_user['total_count']);
    	
    	//获取我的三级同学
    	$sub3_user = $this->User_model->get_user_by_parent3($this->user_data['id']);
    	$this->smarty->assign("sub1_user", $sub3_user['result']);
    	$this->smarty->assign("sub1_user_count", $sub3_user['total_count']);
    	**/

    	//获取我的上级
    	$curr_user = $this->User_model->get_user_by_id($this->user_data['id']);
    	$parent_user = $this->User_model->get_user_by_id($curr_user['parent1']);
		$this->smarty->assign("parent_user",$parent_user);

		//获取学校信息
		$this->load->model('School_model');
		$school = $this->School_model->get_school_by_id($this->school_id);
		$this->smarty->assign("school", $school);

        $this->smarty->assign("count", ceil($sub_user['total_count'] / $this->offset)+1);

    	$this->smarty->assign("menu", "fenxiao");
		$this->smarty->display("user/fx_flist.html");
    }

	function reward(){
		
		$this->smarty->assign("menu", "fenxiao");
		$this->smarty->display("user/fx_wallet.html");
	}

	/**
	 * 分销订单
	 * @return [type] [description]
	 */
	function order($limit=0){
		//取用户账户信息
		$this->load->model('User_model');
		$user_account = $this->User_model->get_user_account_by_id($this->user_data['id']);
		$this->smarty->assign("user_account",$user_account);

		//获取分销订单
		$this->load->model('Order_model');
        if($limit > 0 && is_ajax_request()) $limit = ($limit - 1) * $this->offset;
		$order_result = $this->Order_model->get_fx_order_list_by_user($this->user_data['id'],$limit,$this->offset);
        if(is_ajax_request()){
            if(!empty($order_result['result'])){
                foreach ($order_result['result'] as $key => $value) {
                    $order_result['result'][$key]['add_time'] = date("Y-m-m", $value['add_time']);
                }
                echo json_encode($order_result['result']);
            }else{
                echo json_encode(array());
            }
            exit;
        }
		$this->smarty->assign("order",$order_result['result']);
		$this->smarty->assign("count", ceil($order_result['total_count'] / $this->offset)+1);

		$this->smarty->assign("menu", "fenxiao");
		$this->smarty->display("user/fx_order.html");
	}


}